<script setup lang="ts">
import { ref, onMounted } from "vue";
import { getPaymentConfigs, deletePaymentConfig } from "@/api/payment";
import { ElMessage, ElMessageBox } from "element-plus";

const paymentConfigs = ref<any[]>([]);
const loading = ref(false);

const fetchPaymentConfigs = async () => {
  loading.value = true;
  try {
    const res = await getPaymentConfigs();
    if (res.success) {
      paymentConfigs.value = res.data;
    } else {
      ElMessage.error(res.message || "获取支付配置列表失败");
    }
  } catch (error) {
    ElMessage.error("获取支付配置列表失败");
  } finally {
    loading.value = false;
  }
};

const handleDelete = async (id: number) => {
  ElMessageBox.confirm("确定要删除该支付配置吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(async () => {
    try {
      const res = await deletePaymentConfig(id);
      if (res.success) {
        ElMessage.success("删除成功");
        fetchPaymentConfigs();
      } else {
        ElMessage.error(res.message || "删除失败");
      }
    } catch (error) {
      ElMessage.error("删除失败");
    }
  }).catch(() => {
    // 取消删除
  });
};

onMounted(() => {
  fetchPaymentConfigs();
});
</script>

<template>
  <div class="payment-config-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>支付配置列表</span>
          <el-button type="primary" @click="$router.push('/payment/create')">新增配置</el-button>
        </div>
      </template>
      
      <el-table :data="paymentConfigs" v-loading="loading" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="支付名称" />
        <el-table-column prop="app_id" label="App ID" />
        <el-table-column prop="mch_id" label="商户ID" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" width="180" />
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="$router.push(`/payment/edit/${scope.row.id}`)">
              编辑
            </el-button>
            <el-button link type="danger" size="small" @click="handleDelete(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>